package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.platform.common.Result;
import com.education.platform.entity.AuditLog;
import com.education.platform.service.AuditLogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 审计日志控制器
 *
 * @author education-platform
 * @since 2024-09-27
 */
@Tag(name = "审计日志", description = "审计日志相关接口")
@RestController
@RequestMapping("/api/audit-log")
@RequiredArgsConstructor
public class AuditLogController {

    private final AuditLogService auditLogService;

    @Operation(summary = "获取日志详情", description = "根据ID获取日志详细信息（管理员）")
    @GetMapping("/{id}")
    @SaCheckRole("ADMIN")
    public Result<AuditLog> getAuditLogDetail(@PathVariable Long id) {
        AuditLog auditLog = auditLogService.getAuditLogById(id);
        return Result.success(auditLog);
    }

    @Operation(summary = "分页查询日志", description = "分页查询审计日志（管理员）")
    @GetMapping("/page")
    @SaCheckRole("ADMIN")
    public Result<IPage<AuditLog>> getAuditLogPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) Long adminId,
            @RequestParam(required = false) String action,
            @RequestParam(required = false) String targetType,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Page<AuditLog> page = new Page<>(current, size);
        IPage<AuditLog> result = auditLogService.getAuditLogPage(page, adminId, action, targetType, startTime, endTime);
        return Result.success(result);
    }

    @Operation(summary = "获取管理员操作日志", description = "获取指定管理员的操作日志（管理员）")
    @GetMapping("/admin/{adminId}")
    @SaCheckRole("ADMIN")
    public Result<List<AuditLog>> getLogsByAdminId(@PathVariable Long adminId) {
        List<AuditLog> logs = auditLogService.getLogsByAdminId(adminId);
        return Result.success(logs);
    }

    @Operation(summary = "获取对象操作日志", description = "获取指定对象的操作日志（管理员）")
    @GetMapping("/target/{targetType}/{targetId}")
    @SaCheckRole("ADMIN")
    public Result<List<AuditLog>> getLogsByTarget(@PathVariable String targetType, @PathVariable Long targetId) {
        List<AuditLog> logs = auditLogService.getLogsByTarget(targetType, targetId);
        return Result.success(logs);
    }

    @Operation(summary = "统计操作次数", description = "统计指定操作的次数（管理员）")
    @GetMapping("/count")
    @SaCheckRole("ADMIN")
    public Result<Integer> countByAction(
            @RequestParam String action,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime startTime,
            @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        Integer count = auditLogService.countByAction(action, startTime, endTime);
        return Result.success(count);
    }

    @Operation(summary = "清理过期日志", description = "清理指定天数之前的日志（管理员）")
    @DeleteMapping("/clean")
    @SaCheckRole("ADMIN")
    public Result<Void> cleanOldLogs(@RequestParam(defaultValue = "90") Integer days) {
        auditLogService.cleanOldLogs(days);
        return Result.success();
    }
}

